#!/bin/bash

# Exit as soon as something errors.
set -e

if [[ ! "$_PYAV_ACTIVATED" ]]; then
    export here="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)"
    source "$here/activate.sh"
fi

cd "$PYAV_ROOT"


if [[ ! "$TESTSUITE" ]]; then
    if [[ "$TRAVIS" ]]; then
        TESTSUITE=main
    else
        TESTSUITE=all
    fi
fi


if [[ "$TESTSUITE" == all || "$TESTSUITE" == flake8 ]]; then
    $PYAV_PYTHON -m flake8 av examples tests
    if [[ "$TESTSUITE" != all ]]; then
        # We've only gotten this far if it was successful.
        exit
    fi
fi

if [[ "$TESTSUITE" == all || "$TESTSUITE" == isort ]]; then
    out="$($PYAV_PYTHON -m isort -df -rc -lai 2 examples tests)"
    if [[ "$out" ]]; then
        echo -n "$out"
        exit 1
    fi
    if [[ "$TESTSUITE" != all ]]; then
        exit
    fi
fi

if [[ "$TESTSUITE" == all || "$TESTSUITE" == main ]]; then
    $PYAV_PYTHON setup.py test
fi

if [[ "$TESTSUITE" == sdist ]]; then
    $PYAV_PYTHON setup.py build_ext
    $PYAV_PYTHON setup.py sdist
    if [[ "$TRAVIS_TAG" ]]; then
        $PYAV_PIP install twine
        $PYAV_PYTHON -m twine upload --skip-existing dist/*
    fi
fi
